home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK2.toast / Development Kits (Disc 2) / ScriptX / Draggable ScriptX Folders / utils / widgets / button.sx < prev    next >
Encoding:
Text File  |  1995-10-03  |  6.9 KB  |  279 lines  |  [TEXT/ttxt]

  1. --<<<
  2. class GenericButton (Actuator,TwoDPresenter)
  3. instance vars
  4.     activateAction
  5.     authorData:(undefined)
  6.     _enabled
  7.     pressAction:(undefined)
  8.     releaseAction:(undefined)
  9. end
  10.  
  11. method init self { class GenericButton } #rest args #key\
  12.             enabled: (true)             \
  13.             activateAction:(undefined) ->
  14. (
  15.     apply nextMethod self args
  16.     self._enabled     := enabled 
  17.     self.authorData := undefined
  18.     self.activateAction := activateAction
  19.     self
  20. )
  21.  
  22. method press self {class GenericButton} -> (
  23.     if (self.enabled) do (
  24.         if (self.pressAction != undefined) do
  25.             self.pressAction self.authorData self
  26.         notifyChanged self false
  27.     )
  28.     nextMethod self
  29. )
  30.  
  31. method activate self {class GenericButton} -> (
  32.     if (self.enabled) do (
  33.         if (self.activateAction != undefined) do
  34.             self.activateAction self.authorData self
  35.         notifyChanged self false
  36.     )
  37.     nextMethod self
  38. )
  39.  
  40. method release self {class GenericButton} -> (
  41.     if (self.enabled) do (
  42.         if (self.releaseAction != undefined) do
  43.             self.releaseAction self.authorData self
  44.         notifyChanged self false
  45.     )
  46.     nextMethod self
  47. )
  48.  
  49. method multiactivate self {class GenericButton} n -> (
  50.     if (self.enabled) do (
  51.         activate self
  52.         notifyChanged self false
  53.     )
  54.     nextMethod self n
  55. )
  56.  
  57. method toggleOn self {class GenericButton} ->
  58. (
  59.     if (not self.toggledOn) do (
  60.         NotifyChanged self false
  61.         NextMethod self
  62.     )
  63. )
  64.  
  65. method toggleOff self {class GenericButton} ->
  66. (
  67.     if (self.toggledOn) do (
  68.         NotifyChanged self false
  69.         NextMethod self
  70.     )
  71. )
  72.  
  73. method enabledGetter self {class GenericButton} -> (
  74.     return self._enabled
  75. )
  76.  
  77. method enabledSetter self {class GenericButton} enabled -> (
  78.     if (enabled != self._enabled) do (
  79.         self._enabled := enabled
  80.         notifyChanged self false
  81.     )
  82.     enabled
  83. )
  84.  
  85. class StencilButton (GenericButton)
  86. class variables
  87.     BorderWidth:2
  88. instance variables
  89.     recessedFrame:(new Frame)
  90.     buttonFrame:(new Frame scheme:theButtonScheme)
  91.     stencilTransform:(mutableCopy identityMatrix)
  92.     _stencil
  93.     border:(new Rect)
  94. end
  95.  
  96. method init self { class StencilButton } #rest args #key         \
  97.             stencil:(new Rect x2:10 y2:10)    \
  98.             boundary:                        ->
  99. (
  100.     if (boundary = unsupplied) do ( \
  101.         local inset := (StencilButton.BorderWidth*2 + 2)*2
  102.         if (stencil = undefined) then
  103.             boundary := new rect x2:(10+inset) y2:(10+inset)
  104.         else
  105.             boundary := new rect x2:(stencil.width+inset) \
  106.                 y2:(stencil.height+inset)
  107.     )
  108.     apply nextMethod self boundary:boundary stationary:true args
  109.     self._stencil := stencil
  110. )
  111.  
  112. method recalcRegion self {class StencilButton} ->
  113. (
  114.     nextMethod self
  115.     SetBoundary self.recessedFrame self.boundary
  116.     local border := self.border
  117.     SetTo border self.boundary
  118.     local width := StencilButton.BorderWidth + 1
  119.     InsetRect border width width @mutate
  120.     SetBoundary self.buttonFrame border
  121.     InsetRect border -1 -1 @mutate
  122.     if (self._stencil != undefined) do (
  123.         SetTo self.stencilTransform self.globalTransform
  124.         Translate self.stencilTransform self.bbox.x1 self.bbox.y1
  125.         local xOffset := (self.boundary.width - self._stencil.width)/2
  126.         local yOffset := (self.boundary.height - self._stencil.height)/2
  127.         translate self.stencilTransform xOffset yOffset
  128.     )
  129. )
  130.     
  131. method draw self {class StencilButton} surf clip ->
  132. (
  133.     local transform := self.globalTransform
  134.     fill surf self.globalBoundary clip identityMatrix ColorScheme.grayBrushes[5]
  135.     local drawLowered := self.pressed or self.toggledOn
  136.     if (drawLowered) then
  137.         drawLoweredFrame self.buttonFrame surf clip transform
  138.     else
  139.         drawRaisedFrame self.buttonFrame surf clip transform
  140.     drawLoweredFrame self.recessedFrame surf clip transform
  141.     stroke surf self.border clip transform ColorScheme.grayBrushes[2]
  142.     if (self._stencil != undefined) do (
  143.         transform := self.stencilTransform
  144.         if (drawLowered) do
  145.             translate transform 1 1
  146.         fill surf self._stencil clip transform blackBrush
  147.         if (drawLowered) do
  148.             translate transform -1 -1
  149.     )
  150.     if (not self._enabled) do
  151.         fill surf self.boundary clip self.globalTransform ColorScheme.disableBrush
  152.     NextMethod self surf clip
  153. )
  154.  
  155. method stencilGetter self {class StencilButton} -> self._stencil
  156. method stencilSetter self {class StencilButton} newStencil ->
  157. (
  158.     self._stencil := newStencil
  159.     notifyChanged self true
  160.     newStencil
  161. )
  162.  
  163. class TextButton(StencilButton)
  164. instance variables
  165.     _font
  166.     _text
  167.     textTransform:(mutableCopy identityMatrix)
  168. end
  169.  
  170. method init self { class TextButton } #rest args #key \
  171.     text:("Hello") \
  172.     font:(theSystemFont) \
  173.     stencil:(undefined) \
  174.     boundary: ->
  175. (
  176.     if (not (isAKindOf text String)) do
  177.         text := text as String
  178.     local tStencil := new TextStencil font:font.font \
  179.         size:font.fontSize string:text
  180.     if (boundary = unsupplied) do (
  181.         local stencilWidth,stencilHeight
  182.         if (stencil = undefined) then (
  183.             stencilWidth := 0
  184.             stencilHeight := 0
  185.         )
  186.         else (
  187.             stencilWidth := stencil.width
  188.             stencilHeight := stencil.height
  189.         )
  190.         local frameSpan := (StencilButton.BorderWidth*2 + 1)*2
  191.         local xinset := tStencil.width + frameSpan + 2 + stencilWidth
  192.         local yInset := font.leading + frameSpan + stencilHeight
  193.         boundary := new Rect x2:xInset y2:yInset
  194.     )
  195.     apply nextMethod self boundary:boundary stencil:stencil args
  196.     self._text := tStencil
  197.     self._font := font
  198. )
  199.  
  200. method recalcRegion self {class TextButton} ->
  201. (
  202.     nextMethod self
  203.     local tStencil := self._text
  204.     if (tStencil != undefined) do (
  205.         local textTrans := self.textTransform
  206.         SetTo textTrans self.globalTransform
  207.         local xOffset := (self.boundary.width - tStencil.width)/2
  208.         translate textTrans xOffset \
  209.             (self.border.y2 - (self._font.descent + 2))
  210.         local stencil := self._stencil
  211.         if (stencil != undefined) do (
  212.             local stencilTrans := self.stencilTransform
  213.             local textTop := textTrans.ty - self._font.leading
  214.             local stencilHeight := stencil.height
  215.             if ((stencilTrans.ty + stencilHeight) > textTop) do (
  216.                 stencilTrans.ty := textTop - stencilHeight
  217.             )
  218.         )
  219.     )
  220. )
  221.  
  222. method draw self {class TextButton} surface clip ->
  223. (
  224.     NextMethod self surface clip
  225.     local drawLowered := self.pressed or self.toggledOn
  226.     local tStencil := self._text
  227.     if (tStencil != undefined) do (
  228.         local transform := self.textTransform
  229.         if (drawLowered) do
  230.             translate transform 1 1
  231.         Fill surface tStencil clip transform blackBrush
  232.         if (drawLowered) do
  233.             translate transform -1 -1
  234.         if (not self._enabled) do
  235.             fill surface self.boundary clip self.globalTransform \
  236.                 ColorScheme.disableBrush
  237.     )
  238. )
  239.  
  240. method textGetter self {class TextButton} ->
  241. (
  242.     local tStencil := self._text
  243.     if (tStencil != undefined) then
  244.         tStencil.string
  245.     else
  246.         undefined
  247. )
  248.  
  249. method textSetter self {class TextButton} newString ->
  250. (
  251.     if (self._text = undefined) then
  252.         self._text := new TextStencil string:newString \
  253.             font:self._font.font fontSize:self._font.fontSize
  254.     else
  255.         self._text.string := newString
  256.     notifyChanged self true
  257.     newString
  258. )
  259.  
  260. method fontGetter self {class TextButton} ->
  261. (
  262.     self._font
  263. )
  264.  
  265. method fontSetter self {class TextButton} newFont ->
  266. (
  267.     local tStencil := self._text
  268.     if (tStencil != undefined) do (
  269.         tStencil.font := newFont.font
  270.         tStencil.size := newFont.fontSize
  271.         translate self.textTransform 0 \
  272.             (newFont.leading - self._font.leading)
  273.         notifyChanged self true
  274.     )    
  275.     self._font := newFont
  276. )
  277.  
  278. -->>>
  279.